import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
  baseURL: 'http://localhost:8081/api',
  timeout: 5000,
  withCredentials: true
})

request.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code !== 200) {
      ElMessage.error(res.message || '请求失败')
      return Promise.reject(res)
    }
    return res.data
  },
  error => {
    console.error('请求错误:', error)
    ElMessage.error('网络错误，请检查后端服务是否正常运行')
    return Promise.reject(error)
  }
)

export default request 